import { MyProjectManager } from "../MyProjectManager";
import { Destroyable, react, reactJson, Event, track, HasOwner, createNextAnimateFrameEvent } from "xbsj-xe2/dist-node/xe2-base-utils";
import { ObjResettingWithEvent, SceneObject } from 'xbsj-xe2/dist-node/xe2-base-objects';
import { ElMessage } from "element-plus";
import { ESFeTreeAdding } from "../esobjs/ESFeTreeAdding";
import { getsceneObjNumfromSceneTree } from "../utils";
import { ESJTree } from "../esobjs/ESForest";

class PointsChoose extends Destroyable {
    constructor(addTree: AddTree, owner: MyProjectManager) {
        super();
        const viewer = owner.activeViewer
        if (!viewer) return;
        if (!addTree.currentTreeModelClassName) {
            ElMessage.error('请先选择树种')
            addTree.isHighlight = ""
            return;
        }
        this.dispose(viewer.clickEvent.don(async e => {
            if (!e.screenPosition) return;
            const result = await viewer.pickPosition(e.screenPosition) as any

            if (!result || this.isDestroyed()) return;

            const sceneObject = owner.createSceneObject(ESFeTreeAdding) as ESFeTreeAdding
            sceneObject.position = result
            sceneObject.plotId = owner.eSForestManager.esForestPlotId
            sceneObject.time = owner.eSForestManager.esForestTime

            sceneObject.treeModelClassName = addTree.currentTreeModelClassName
            sceneObject.shugao = addTree.shugao
            sceneObject.xiongjing = addTree.xiongjing
            sceneObject.zhixiagao = addTree.zhixiagao
            sceneObject.dongxiguanfu = addTree.dongxiguanfu
            sceneObject.nanbeiguanfu = addTree.nanbeiguanfu
            const param = addTree.setTreeModelClasses(sceneObject.json) as any
            sceneObject.url = param.url
            sceneObject.scale = param.scale

            const treeItem = owner.createdTreeItem(owner.forestSceneTreeWrapper)
            treeItem.sceneObject = sceneObject;

            const name = addTree.currentTreeModelClassName
            const num = getsceneObjNumfromSceneTree(sceneObject.typeName, owner.forestSceneTreeWrapper);
            treeItem.name = `新增_${name}(${num})`


        }))
    }
}
class LinesChoose extends Destroyable {
    constructor(addTree: AddTree, owner: MyProjectManager) {
        super();
        const viewer = owner.activeViewer
        if (!viewer) return;
        if (!addTree.currentTreeModelClassName) {
            ElMessage.error('请先选择树种')
            addTree.isHighlight = ""
            return;
        }
    }
}
class PlaneChoose extends Destroyable {
    constructor(addTree: AddTree, owner: MyProjectManager) {
        super();
        const viewer = owner.activeViewer
        if (!viewer) return;
        if (!addTree.currentTreeModelClassName) {
            ElMessage.error('请先选择树种')
            addTree.isHighlight = ""
            return;
        }
    }
}


export class AddTree extends HasOwner<MyProjectManager> {
    // 标记点击
    private _isHighlight = this.dv(react<string>(""));
    get isHighlight() { return this._isHighlight.value; }
    set isHighlight(value) { this._isHighlight.value = value }
    get isHighlightChanged() { return this._isHighlight.changed; }

    // 树种
    private _treeModelClasses = this.dv(reactJson<any>([]));
    get treeModelClasses() { return this._treeModelClasses.value; }
    set treeModelClasses(value) { this._treeModelClasses.value = value }
    get treeModelClassesChanged() { return this._treeModelClasses.changed; }

    // 当前选中树种
    private _currentTreeModelClassName = this.dv(react<string>(""));
    get currentTreeModelClassName() { return this._currentTreeModelClassName.value; }
    set currentTreeModelClassName(value) { this._currentTreeModelClassName.value = value }
    get currentTreeModelClassNameChanged() { return this._currentTreeModelClassName.changed; }

    // 树高
    private _shugao = this.dv(react<number>(0));
    get shugao() { return this._shugao.value; }
    set shugao(value) { this._shugao.value = value }
    get shugaoChanged() { return this._shugao.changed; }

    // 胸径
    private _xiongjing = this.dv(react<number>(0));
    get xiongjing() { return this._xiongjing.value; }
    set xiongjing(value) { this._xiongjing.value = value }
    get xiongjingChanged() { return this._xiongjing.changed; }

    // 枝下高
    private _zhixiagao = this.dv(react<number>(0));
    get zhixiagao() { return this._zhixiagao.value; }
    set zhixiagao(value) { this._zhixiagao.value = value }
    get zhixiagaoChanged() { return this._zhixiagao.changed; }

    // 东西冠幅
    private _dongxiguanfu = this.dv(react<number>(0));
    get dongxiguanfu() { return this._dongxiguanfu.value; }
    set dongxiguanfu(value) { this._dongxiguanfu.value = value }
    get dongxiguanfuChanged() { return this._dongxiguanfu.changed; }

    // 南北冠幅
    private _nanbeiguanfu = this.dv(react<number>(0));
    get nanbeiguanfu() { return this._nanbeiguanfu.value; }
    set nanbeiguanfu(value) { this._nanbeiguanfu.value = value }
    get nanbeiguanfuChanged() { return this._nanbeiguanfu.changed; }

    clear() {
        this.isHighlight = ""
    }

    // 设置树种模型
    setTreeModelClasses(json: any) {
        const viewer = this.owner.activeViewer
        if (!viewer) return;
        const trees = [{
            "id": json.id,
            "treeType": json.treeModelClassName,
            "position": json.position,
            "guanfu": [
                json.dongxiguanfu,
                json.nanbeiguanfu
            ],
            "xiongjing": json.xiongjing,
            "zhixiagao": json.zhixiagao,
            "shugao": json.shugao
        }] as ESJTree[];
        const treeTypes = this.owner.eSForestManager.eSForest.getTreeTypes()
        const classes = this.owner.eSForestManager.eSForest.formatData(trees, treeTypes)
        const currentTreeModel = treeTypes[classes[0].treeTypeId]
        return {
            scale: classes[0].scale,
            // url: viewer.typeName == "ESCzmViewer" ? currentTreeModel.cesiumAssetServerPath : currentTreeModel.ueAssetServerPath
            url: currentTreeModel.cesiumAssetServerPath 
        }

    }


    /**
     *  拾取点处理
     * createNextAnimateFrameEvent再次触发settingwithEvent
     */
    private _event = this.dv(createNextAnimateFrameEvent(this.isHighlightChanged));
    private _pickSetting = this.dv(new ObjResettingWithEvent(this._event, () => {

        if (!this.isHighlight) {
            return undefined
        }
        switch (this.isHighlight) {
            case 'point':
                return new PointsChoose(this, this.owner);
            case 'line':
                return new LinesChoose(this, this.owner);
            case 'plane':
                return new PlaneChoose(this, this.owner);
            default: return undefined
        }

    }))

    constructor(owner: MyProjectManager) {
        super(owner);

        {
            const update = () => {
                if (!this.currentTreeModelClassName) {
                    this.shugao = 0
                    this.xiongjing = 0
                    this.zhixiagao = 0
                    this.dongxiguanfu = 0
                    this.nanbeiguanfu = 0
                    return;
                }    
                const treeTypes = this.owner.eSForestManager.eSForest.treeTypes
                const { shugao, xiongjing, zhixiagao, dongxiguanfu, nanbeiguanfu } = treeTypes[this.currentTreeModelClassName][0]
                this.shugao = shugao
                this.xiongjing = xiongjing
                this.zhixiagao = zhixiagao
                this.dongxiguanfu = dongxiguanfu
                this.nanbeiguanfu = nanbeiguanfu
            }
            this.dispose(this.currentTreeModelClassNameChanged.disposableOn(update))
        }
    }
}